<?php
/*
 * Joomla! 1.5 component ArticleCashRewards
 *
 * @version $Id: listing.php 2009-05-10 01:47:01 svn $
 * @author Maverick
 * @package controllers
 * @subpackage ArticleCashRewards
 * @license GNU/GPL
 * Website: http://www.boddunan.com
 *
 * Review and approve the articles and assign points and cash rewards.
 * Users can see their articles and the points/cash rewards assigned in the front-end module.
 */

defined('_JEXEC') or die();

class ArticleCashRewardsControllerAwards extends ArticleCashRewardsController
{

	function __construct(){
		parent::__construct();
        $this->registerTask( 'adhoc','awardUsersPage' );
		$this->registerTask( 'save', 'save' );
		$this->registerTask( 'publish', 'publish' );
		$this->registerTask( 'unpublish', 'unpublish' );
		$this->registerTask( 'remove', 'remove' );
		$this->registerTask( 'save_type', 'saveType' );
        $this->registerTask( 'manage','manage' );
        $this->registerTask( 'list','viewList' );
        $this->registerTask( 'getusers','getUsers' );
        $this->registerTask( 'cPanel','cPanel' );
	}

    function awardUsersPage(){
//        JRequest::setVar( 'view', 'adhoc' );
        $view = & $this->getView('awards', 'html');
		$model = & $this->getModel('awards');
		$view->setModel($model, true);
        $view->setLayout('form');
        $view->display();
    }

    function manage(){
//        JRequest::setVar( 'view', 'adhoc' );
        $view = & $this->getView('awards', 'html');
		$model = & $this->getModel('awards');
		$view->setModel($model, true);
        $view->setLayout('manage');
        $view->display();
    }

	function save() {

		$model = $this->getModel('awards');

		// bind the array to the model and save it.
		if ($model->save()){
			$message = JText::_('Award Payment Success. ');
            $model = & $this->getModel('reports');
            $author_id      = JRequest::getVar( 'author_ids', null, 'post', 'ARRAY');
            if($model->recalculateEarnings($author_id)){
                $message = JText::_('Global reports updated successfully.');
            }else{
                $message = JText::_('Global reports updation failed.');
                $message .= ' ['.$model->getError().'] ';
            }            
		} else {
			$message = JText::_('Failed to make payment');
			$message .= ' ['.$model->getError().'] ';
		}

        $this->setRedirect('index.php?option=com_articlecashrewards&controller=awards&task=adhoc', $message);
	}

	function saveType() {

		$model = $this->getModel('awards');

		// bind the array to the model and save it.
		if ($model->saveType()){
			$message = JText::_('Adhoc type Successfully added/updated. ');
		} else {
			$message = JText::_('Failed to add/update adhoc type');
			$message .= ' ['.$model->getError().'] ';
		}

        $this->setRedirect('index.php?option=com_articlecashrewards&controller=awards&task=manage', $message);
	}

    function viewList(){
        $view = & $this->getView('awards', 'html');
		$model = & $this->getModel('awards');
		$view->setModel($model, true);
        $view->setLayout('list');
        $view->display();
	}

    function getUsers(){
        global $mainframe;
        $keyword = JRequest::getVar( 'keyword');
        $model = $this->getModel('awards');
        $users = $model->getUsers($keyword);

        $return = "<?xml version=\"1.0\" encoding=\"utf8\" ?>";
        $return .= "<items>";
        if($users) {
            foreach ($users as $user) {
                $return .="<item>" . $user->id . "|" . JText::_($user->username) . "|" . JText::_($user->name) . "</item>";
            }
        }
        $return .= "</items>";
        echo $return;
        $mainframe->close();
    }

    function publish(){
		$model = $this->getModel('awards');
        $total = $model->stateChange(1);
		if ($total){
			$message = $total . JText::_(' items published. ');
		} else {
			$message = JText::_('Failed to publish');
			$message .= ' ['.$model->getError().'] ';
		}

        $this->setRedirect('index.php?option=com_articlecashrewards&controller=awards&task=manage', $message);
    }

    function unpublish(){
		$model = $this->getModel('awards');
        $total = $model->stateChange(0);
		if ($total){
			$message = $total . JText::_(' items unpublished. ');
		} else {
			$message = JText::_('Failed to unpublish');
			$message .= ' ['.$model->getError().'] ';
		}

        $this->setRedirect('index.php?option=com_articlecashrewards&controller=awards&task=manage', $message);
    }

    function remove(){
		$model = $this->getModel('awards');
        $total = $model->stateChange(2);
		if ($total){
			$message = $total. JText::_(' items removed. ');
		} else {
			$message = JText::_('Failed to remove.');
			$message .= ' ['.$model->getError().'] ';
		}

        $this->setRedirect('index.php?option=com_articlecashrewards&controller=awards&task=manage', $message);
    }

    function cPanel(){
        $link = 'index.php?option=com_articlecashrewards&controller=cpanel';
        $this->setRedirect($link);
    }
}
?>
